Skip to content

Method: fixedInteger(String, Integer)

1: /*
2: * Copyright © 2020-2023 Fachhochschule für die Wirtschaft (FHDW) Hannover
3: *
4: * This file is part of gaming-core.
5: *
6: * Gaming-core is free software: you can redistribute it and/or modify it under
7: * the terms of the GNU General Public License as published by the Free Software
8: * Foundation, either version 3 of the License, or (at your option) any later
9: * version.
10: *
11: * Gaming-core is distributed in the hope that it will be useful, but WITHOUT
12: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13: * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
14: * details.
15: *
16: * You should have received a copy of the GNU General Public License along with
17: * gaming-core. If not, see <http://www.gnu.org/licenses/>.
18: */
19: package de.fhdw.gaming.core.ui.util;
20:
21: import java.util.LinkedHashMap;
22: import java.util.Map;
23: import java.util.Optional;
24: import java.util.Set;
25:
26: import de.fhdw.gaming.core.domain.GameBuilder;
27: import de.fhdw.gaming.core.ui.InputProvider;
28: import de.fhdw.gaming.core.ui.type.BooleanFieldType;
29: import de.fhdw.gaming.core.ui.type.FieldType;
30: import de.fhdw.gaming.core.ui.type.IntegerFieldType;
31: import de.fhdw.gaming.core.ui.type.ObjectFieldType;
32: import de.fhdw.gaming.core.ui.type.StringFieldType;
33: import de.fhdw.gaming.core.ui.type.validator.Validator;
34:
35: /**
36: * A non-interactive {@link InputProvider} implementation returning predefined data only.
37: * <p>
38: * Unlike interactive input providers, it is possible to create chains of {@link NonInteractiveInputProvider}s in
39: * advance. This is helpful e.g. when creating a {@link GameBuilder}, as building a game requires parameters for the
40: * game as a whole as well as parameters for each participating player. Each parameter set is expected to be returned by
41: * a separate {@link InputProvider}.
42: */
43: public final class NonInteractiveInputProvider implements InputProvider {
44:
45: /**
46: * The provided data.
47: */
48: private final Map<String, Object> providedData;
49: /**
50: * The needed data.
51: */
52: private final Map<String, FieldType<?>> neededData;
53:
54: /**
55: * Creates a {@link NonInteractiveInputProvider}.
56: */
57: public NonInteractiveInputProvider() {
58: this.providedData = new LinkedHashMap<>();
59: this.neededData = new LinkedHashMap<>();
60: }
61:
62: @Override
63: @SafeVarargs
64: public final NonInteractiveInputProvider needString(final String id, final String prompt,
65: final Optional<String> defaultValue, final Validator<String>... validators) {
66:
67: this.neededData.put(id, new StringFieldType(defaultValue).validateBy(validators));
68: return this;
69: }
70:
71: @Override
72: public NonInteractiveInputProvider fixedString(final String id, final String fixedValue) {
73: this.providedData.put(id, fixedValue);
74: return this;
75: }
76:
77: @Override
78: @SafeVarargs
79: public final NonInteractiveInputProvider needInteger(final String id, final String prompt,
80: final Optional<Integer> defaultValue, final Validator<Integer>... validators) {
81:
82: this.neededData.put(id, new IntegerFieldType(defaultValue).validateBy(validators));
83: return this;
84: }
85:
86: @Override
87: public NonInteractiveInputProvider fixedInteger(final String id, final Integer fixedValue) {
88: this.providedData.put(id, fixedValue);
89: return this;
90: }
91:
92: @Override
93: @SafeVarargs
94: public final NonInteractiveInputProvider needBoolean(final String id, final String prompt,
95: final Optional<Boolean> defaultValue, final Validator<Boolean>... validators) {
96:
97: this.neededData.put(id, new BooleanFieldType(defaultValue).validateBy(validators));
98: return this;
99: }
100:
101: @Override
102: public NonInteractiveInputProvider fixedBoolean(final String id, final Boolean fixedValue) {
103: this.providedData.put(id, fixedValue);
104: return this;
105: }
106:
107: @Override
108: public NonInteractiveInputProvider needObject(final String id, final String prompt,
109: final Optional<Object> defaultValue,
110: final Set<?> objectSet) {
111:
112: this.neededData.put(id, new ObjectFieldType(defaultValue, objectSet));
113: return this;
114: }
115:
116: @Override
117: public NonInteractiveInputProvider fixedObject(final String id, final Object fixedValue) {
118: this.providedData.put(id, fixedValue);
119: return this;
120: }
121:
122: /**
123: * {@inheritDoc}
124: * <p>
125: * Note that if no predefined value is available for a given field, {@code null} is stored.
126: */
127: @Override
128: public Map<String, Object> requestData(final String title) {
129: final Map<String, Object> result = new LinkedHashMap<>();
130: for (final Map.Entry<String, FieldType<?>> entry : this.neededData.entrySet()) {
131: final FieldType<?> fieldType = entry.getValue();
132: result.put(
133: entry.getKey(),
134: this.providedData.getOrDefault(entry.getKey(), fieldType.getDefaultValue().orElse(null)));
135: }
136:
137: return result;
138: }
139:
140: @Override
141: public NonInteractiveInputProvider getNext(final Map<String, Object> lastDataSet) {
142: return new NonInteractiveInputProvider();
143: }
144: }